# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from pymongo import MongoClient

from MySpiders.items import DoubanbookItem, LaGouItem, DyItem


class SpiderPipeline(object):
    def __init__(self):
        connection = MongoClient("localhost", 27017)
        self.db = connection["douban"]
        self.bookover9 = self.db["bookover9"]
        self.lagou = self.db["lagou"]
        self.dylol = self.db["dylol"]

    def saveOrUpdate(self,collection,item):
        _id= dict(item).get("_id")

        if _id is not None:
            tmp=collection.find_one({"_id":_id})
            #数据库不存在
            if tmp is None:
                print _id
                collection.insert(dict(item))
                #TODO 暂时只插入不更新
            # else:
            #     collection.update({"_id":_id},dict(item))
        else:
            collection.insert(dict(item))

    def process_item(self, item, spider):
        if isinstance(item,DoubanbookItem):
            self.saveOrUpdate(self.bookover9,item)
        elif isinstance(item,LaGouItem):
            self.saveOrUpdate(self.lagou,item)
        elif isinstance(item,DyItem):
            self.saveOrUpdate(self.dylol,item)
        return item